package com.telecom.weixi.parse;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.telecom.weixi.util.Utils;

public class ServBaseParse2 extends DefaultHandler {

	ArrayList<ServBaseInfo2> infoList;
	ServBaseInfo2 infoItem;
	StringBuffer buffer;

	private static final String Tag_Head = "ServBaseBean";
	private static final String Tag_acc_nbr = "acc_nbr";
	private static final String Tag_cust_id = "cust_id";
	private static final String Tag_serv_id = "serv_id";
	private static final String Tag_serv_name = "serv_name";
	private static final String Tag_acct_arpu = "acctarpu";
	private static final String Tag_manager = "chnl_manager_name";
	private static final String Tag_month_flow = "cur_mon_flow";
	private static final String Tag_brand_name = "cust_sub_brand_name";
	private static final String Tag_serv_num = "join_acct_serv_num";
	private static final String Tag_join_date = "join_date";
	private static final String Tag_last_month = "last_mon_flow";
	private static final String Tag_pay_bal = "pre_pay_bal";
	private static final String Tag_cust_type = "serv_cust_type_name";
	private static final String Tag_serv_state = "serv_state_name";
	private static final String Tag_trait_name = "serv_trait_name";
	private static final String Tag_latn_name = "latn_name";

	public ArrayList<ServBaseInfo2> getData() {
		return infoList;
	}

	public void Debug() {
		Utils.debug("test", "ServBaseParse2 ...");
		for (ServBaseInfo2 item : infoList) {
			Utils.debug("test", item.toString());
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		buffer.append(ch, start, length);
		super.characters(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		if (localName.equalsIgnoreCase(Tag_Head)) {
			infoList.add(infoItem);
			Utils.debug("test", infoList.size() + ": " + infoItem.toString());
		} else if (localName.equalsIgnoreCase(Tag_acc_nbr)) {
			infoItem.setAccNbr(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_cust_id)) {
			infoItem.setCustId(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_serv_id)) {
			infoItem.setServId(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_serv_name)) {
			infoItem.setServName(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_acct_arpu)) {
			infoItem.setArpu(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_manager)) {
			infoItem.setManager(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_month_flow)) {
			infoItem.setMonFlow(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_brand_name)) {
			infoItem.setBrandName(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_serv_num)) {
			infoItem.setAcctNum(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_join_date)) {
			infoItem.setJoinDate(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_last_month)) {
			infoItem.setLastFlow(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_pay_bal)) {
			infoItem.setPayBal(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_cust_type)) {
			infoItem.setCustType(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_serv_state)) {
			infoItem.setState(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_trait_name)) {
			infoItem.setTraitName(buffer.toString().trim());
			buffer.setLength(0);
		} else if (localName.equalsIgnoreCase(Tag_latn_name)) {
			infoItem.setLatn_name(buffer.toString().trim());
			buffer.setLength(0);
		}
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		if (infoList == null) {
			infoList = new ArrayList<ServBaseInfo2>();
		}
		buffer = new StringBuffer();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
		if (localName.equalsIgnoreCase(Tag_Head)) {
			infoItem = new ServBaseInfo2();
		}
	}

}
